<?php
/**
 * @file
 * All action/callback related stuff
 * --> facade <--
 */

function _show_user_front($node) {
  $node = race_view($node);
  $html = "<br />". drupal_render($node->content);
  return $html;
}

function _show_admin_front($node) {
  return "<br />". _race_admin($node);
}

/**
 * Form-callback to sign up
 */
function _sign_on($nid, $values) {
  global $user;
  $node = node_load($nid);
  $signups = _get_signups($node);
  $config = _get_config($node);
  if (!_is_user_signed_up($signups, $user->name)) {
	_add_user_element($signups, $user->name, $config, $values);
	_save_signups($signups, $node);
  }
  return _show_user_front($node);
}

/**
 * Callback to sign off
 */
function race_signoff($nid) {
  global $user;
  $node = node_load($nid);
  $signups = _get_signups($node);
  if (_is_user_signed_up($signups, $user->name)) {
	$signups = _remove_user_signup($signups, $user->name);
	_save_signups($signups, $node);
  }
  return _show_user_front($node);
}

function race_signup_for_boat($nid, $boatId) {
  global $user;
  $node = node_load($nid);
  $signups = _get_signups($node);
  $config = _get_config($node);
  $assignments = _get_assignments($node);
  
  if (!_is_user_signed_up($signups, $user->name)) {
	_add_user_element($signups, $user->name, $config);
    _save_signups($signups, $node);
  }

  _assign_user_to_boat($assignments, $config, $user->name, $boatId);
  _save_assignments($assignments, $node);
  
  return _show_user_front($node);
}

function race_signoff_from_boat($nid, $boatId) {
  global $user;
  $node = node_load($nid);
  $signups = _get_signups($node);
  $assignments = _get_assignments($node);
  
  $assignments = _remove_user_from_boat($assignments, $user->name, $boatId);
  _save_assignments($assignments, $node);
  
  if (!_is_already_assigned($assignments, $user->name)) {
    $signups = _remove_user_signup($signups, $user->name);
    _save_signups($signups, $node);
  }

  return _show_user_front($node);
}

/**
 * Callback to automatically assing sign-ups to boats.
 */
function race_automatic_assign($nid) {
  global $user;
  $node = node_load($nid);
  $signups = _get_signups($node);
  $assignments = _get_assignments($node);
  $config = _get_config($node);
  _automatic_assign($signups, $assignments, $config);
  _save_assignments($assignments, $node);
  return _show_admin_front($node);
}

function race_assign($nid, $userName, $boatId)  {
  global $user;
  $node = node_load($nid);
  $assignments = _get_assignments($node);
  $config = _get_config($node);
  $signups = _get_signups($node);
  
  if(!_is_user_signed_up($signups, $userName)) throw new Exception("Can not assign a user that is not signed up.");
  if($userName != $user->name && !user_access('admin')) throw new Exception("You're not allowed to assign a user to a boat.");
  
  if(_is_already_assigned($assignments, $user->name)) {
    $assignments = _delete_assignments_of_user($assignments, $user->name);
  }
  _assign_user_to_boat($assignments, $config, $userName, $boatId);
  _save_assignments($assignments, $node);
  return _show_admin_front($node);
}

function race_unassign($nid, $userName, $boatId)  {
  global $user;
  $node = node_load($nid);
  $assignments = _get_assignments($node);

  if($userName != $user->name && !user_access('admin')) throw new Exception("You're not allowed to unassign a user from a boat.");
  
  $assignments = _remove_user_from_boat($assignments, $userName, $boatId);
  _save_assignments($assignments, $node);
  
  return _show_admin_front($node);
}

function race_reject($nid, $userName) {
  $node = node_load($nid);
  $assignments = _get_assignments($node);
  $signups = _get_signups($node);
  $config = _get_config($node);
  
  if (_is_already_assigned($assignments, $userName)) {
    throw new Exception("Can not reject assigned user.");
  } else {
    $signups = _remove_user_signup($signups, $userName);
    _save_signups($signups, $node);
  }
   
  $rejectedUser = user_load("name", $userName);
  $rejectionConfig = _get_rejection_element($config);
  $message['id'] = "race_reject";
  $message['to'] = $rejectedUser['mail'];
  $message['subject'] = $rejectionConfig['subject'];
  $message['body'] = (String) $rejectionConfig;
  $message['headers']['From'] = $rejectionConfig['from'];
  $message['headers']['Cc'] = $rejectionConfig['cc'];
  if (!drupal_mail_send($message)) {
    echo "Sending mail failed. Var_dump of message:";
    var_dump($message);
  }

  return _show_admin_front($node);
}

function race_change_mode($nid) {
  $node = node_load($nid);
  $current = $node->state;
  if ($current == Constants::$STATE_SIGNUP) {
    $node->state = Constants::$STATE_ASSIGNMENT;
  } else {
    $node->state = Constants::$STATE_SIGNUP;
  }
  race_update($node);
  return _show_admin_front($node);
}

function race_reset($nid) {
  $node = node_load($nid);
  
  $signups = _get_signups($node);
  $signups = _remove_unfixed_signups($signups);
  _save_signups($signups, $node);

  $assignments = _get_assignments($node);
  $assignments = _remove_unfixed_assignments($assignments);
  _save_assignments($assignments, $node);
  
  return _show_admin_front($node);
}
